<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
</head>
<body>
<input type="text" id="i1">
<input type="text" id="i2">
<input type="button" value="加" onclick="add()">
<input type="button" value="减" onclick="sub()">
<input type="button" value="乘" onclick="mul()">
<input type="button" value="除" onclick="div()">
<div id="d1">结果是:</div>
<hr>
<hr>
<hr>
<input type="text" id="i11">
<input type="text" id="i22">
<input type="button" value="加" onclick="f(1)">
<input type="button" value="减" onclick="f(2)">
<input type="button" value="乘" onclick="f(3)">
<input type="button" value="除" onclick="f(4)">
<div id="d11">结果是:</div>
<script>
    function add() {
        let num1 = document.querySelector("#i1");
        let num2 = document.querySelector("#i2");
        let d = document.querySelector("#d1");
        if(isNaN(num1.value)||isNaN(num2.value)){
            d.innerText = "输入的信息有误!";
            return;
        }
        d.innerText = "结果是:"+(num1.value*1+num2.value*1);
    }
    function sub() {
        let num1 = document.querySelector("#i1");
        let num2 = document.querySelector("#i2");
        let d = document.querySelector("#d1");
        if(isNaN(num1.value)||isNaN(num2.value)){
            d.innerText = "输入的信息有误!";
            return;
        }
        d.innerText = "结果是:"+(num1.value-num2.value);
    }
    function mul() {
        let num1 = document.querySelector("#i1");
        let num2 = document.querySelector("#i2");
        let d = document.querySelector("#d1");
        if(isNaN(num1.value)||isNaN(num2.value)){
            d.innerText = "输入的信息有误!";
            return;
        }
        d.innerText = "结果是:"+(num1.value*num2.value);
    }
    function div() {
        let num1 = document.querySelector("#i1");
        let num2 = document.querySelector("#i2");
        let d = document.querySelector("#d1");
        if(isNaN(num1.value)||isNaN(num2.value)){
            d.innerText = "输入的信息有误!";
            return;
        }
        d.innerText = "结果是:"+(num1.value/num2.value);
    }

    function f(x) {
        let num1 = document.querySelector("#i11");
        let num2 = document.querySelector("#i22");
        let d = document.querySelector("#d11");
        if(isNaN(num1.value)||isNaN(num2.value)){
            d.innerText = "输入的信息有误!";
            return;
        }
        switch (x) {
            case 1:
                d.innerText = "结果是:"+(num1.value*1+num2.value*1);
                // break;
                alert("加")
            case 2:
                d.innerText = "结果是:"+(num1.value-num2.value);
                // break;
                alert("减")
            case 3:
                d.innerText = "结果是:"+(num1.value*num2.value);
                break;
            case 4:
                d.innerText = "结果是:"+(num1.value/num2.value);
                break;
        }
    }
</script>
</body>
</html>